﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;
using BarTender;
using System.Text;
using Newtonsoft.Json;


namespace bartenderService
{
    /// <summary>
    /// BartenderWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]

    public class SysContext
    {
        public static BarTender.Application btapp = new BarTender.Application();
        public static BarTender.Format btfat;
    }
    public class BartenderWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }


        [WebMethod]
        public string GetTestLabelJson()
        {
            printContext pt=new printContext();
            pt.data = new List<printCell>();
            printCell cell=new printCell();
            pt.modelName = "气体检测仪.btw";
            pt.printDateStr = "生产日期";
            pt.printFBStr = "防爆证号";
            pt.printGasStr = "测量气体";
            pt.printSNStr = "产品编号";
            pt.printTELStr = "热线电话";
            pt.printVolStr = "工作电压";
            pt.printRangeStr = "检测范围";
            cell.printDate = "2019.12";
            cell.printFB = "CNEX17.0263";
            cell.printGas = "可燃气体";
            cell.printRange = "0-100";
            cell.printVol = "24V";
            cell.printUnit = "%LEL";
            cell.printSN = "226013221";
            pt.data.Add(cell);
            cell.printGas = "氨气";
            cell.printUnit = "PPM";
            pt.data.Add(cell);
            return JsonConvert.SerializeObject(pt);
        }

        [WebMethod]
        public string PrintLabel(string jsonstr)
        {
            int printcount = 0,i=0;
            string path = System.AppDomain.CurrentDomain.BaseDirectory + "model";
            string file_ini = path + "\\configure.ini";
            //pt.modelName + pt.data[1].printRange + pt.data[1].printGas
            //Newtonsoft.Json.Linq.JArray.Parse(jsonstr);
            printContext pt = JsonConvert.DeserializeObject<printContext>(jsonstr);
            if (pt.modelName == null)
                pt.modelName = "";
            if (pt.modelName == "")
                pt.modelName = "气体检测仪.btw";
            try
            {
                SysContext.btfat = SysContext.btapp.Formats.Open(path + "\\" + pt.modelName, false, "");
            }
            catch
            {
                pt.modelName = "气体检测仪.btw";
                SysContext.btfat = SysContext.btapp.Formats.Open(path + "\\" + pt.modelName, false, "");
            }
            SysContext.btfat.PrintSetup.IdenticalCopiesOfLabel = 1;
            SysContext.btapp.Visible = false;
            printcount = pt.data.Count();
            for(i=0;i<printcount;i++)
            {
                SysContext.btfat.SetNamedSubStringValue("BT_Gas", String.Format(pt.printGasStr+"："+pt.data[i].printGas));
                SysContext.btfat.SetNamedSubStringValue("BT_Range", String.Format(pt.printRangeStr+"："+pt.data[i].printRange+ pt.data[i].printUnit));
                SysContext.btfat.SetNamedSubStringValue("BT_Vol", String.Format(pt.printVolStr + "：" + pt.data[i].printVol));
                SysContext.btfat.SetNamedSubStringValue("BT_SN", String.Format(pt.printSNStr + "：" + pt.data[i].printSN));
                SysContext.btfat.SetNamedSubStringValue("BT_DATE", String.Format(pt.printDateStr + "：" + pt.data[i].printDate));
                SysContext.btfat.SetNamedSubStringValue("BT_FB", String.Format(pt.printFBStr + "：" + pt.data[i].printFB));
                if(pt.data[i].printTEL!=""&& pt.data[i].printTEL !=null)
                    SysContext.btfat.SetNamedSubStringValue("BT_TEL", String.Format(pt.printTELStr + "：" + pt.data[i].printTEL));
                SysContext.btfat.PrintOut(false, false);
            } 
            return "ok";
        }

    }
}
